var app = getApp();
var util = require('../../../utils/util.js');
function findKey (obj,value, compare = (a, b) => a === b) {
  return Object.keys(obj).find(k => compare(obj[k], value))>0?Object.keys(obj).find(k => compare(obj[k], value)):0
}
var y,y1,y2,y3;
var PAGE_W = wx.getSystemInfoSync().windowWidth;
// 中文转全拼
var ZXZ={};
var Font_Lib=[];
app.AppHttp('agreemanage/strChineseAllPY.json',{}, function(res) {
  Font_Lib=res.data;    
});;
ZXZ.widget={};
ZXZ.widget.pinyin = {convertPY: function(chrStr) {
    if (chrStr == null || chrStr.length == 0)
        return "";
    var tmpchr = chrStr.charAt(0);
    if (chrStr.charCodeAt(0) <= 255)
        return tmpchr;
    for (var i = 0; i < Font_Lib[1].length; i++) {
        if (Font_Lib[1][i].indexOf(tmpchr) >= 0)
            return Font_Lib[0][i];
    }
    return "";
},convertPYs: function(str) {
    var arr = str.split("");
    var arrPY = [], arrPYS = [];
    var ssht;
    for (var i = 0; i < arr.length; i++) {
        ssht = this.convertPY(arr[i]);
        if (ssht) {
            arrPY.push(ssht);
            arrPYS.push(ssht.charAt(0).toUpperCase());
        }
    }
    return [arrPY.join(""), arrPYS.join("")];
}};
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodList:[],
    showModal: false,  //订单备注信息弹窗显隐
    isInput: false,   //添加商品弹窗
    showMask:false,   //底部按钮显隐
    inputNum:'',    //添加商品输入的数量
    order_id:'',   //订单id
    order_remark:'',  //订单备注信息输入内容
    istszh: false,   //特殊备注弹窗
    tszhcontent: "",   //备注内容
    ischoose_index:-1,   //选中规格样式 
    pr_info: "",
    aux_pf_list:[], //gy_pf辅助单位列表
    aux_pf_index:-1, //gy_pf辅助单位索引
    is_move_gif:false,  //按钮动图
    KeyboardKeys: [1, 2, 3, 4, 5, 6, 7, 8, 9, '·', 0], //键盘
    shangpin:'', //替换商品搜索value
    is_replace:0, //0关闭，2替换
    goodList:[],  //商品列表
    goodList_obj:{},  //商品是否已添加(查询表)
    isEditSort:false,
    move_index:-1,   //拖拽索引
    move_y: '',
    is_create_success:false, //下单成功窗
    item_h:160/750*PAGE_W
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options){
    wx.setNavigationBarColor({ 
      frontColor: '#ffffff',
      backgroundColor:'#19a751'
    });
    wx.setNavigationBarTitle({
      title:options.supplier_name
    });
    this.setData({
      supplier_id:options.supplier_id,
      is_store_disable:JSON.parse(options.is_store_disable), //供应商目录是否禁止
      good_dir_set:options.good_dir_set,  //目录类型
      client_is_store:options.client_is_store, //<0未加入集团，==0加入集团无库 ,==1加入集团有库
      purchase_type:options.purchase_type, //1门店采购，2总部采购
      price_type:options.price_type,  //客户价格模式
      is_again:options.is_again //是否为再来一单
    });
    this.get_record();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getUnitList();
  },
  // 获取单位列表
  getUnitList(){
    let that = this;
    app.AppHttp('api/Supplier/unit_list', {}, function(res) {
      that.setData({
        unit_list: res.data
      });
    });
  },
  // 再来一单处理
  again_fun(){
    let that = this;
    let postDataArr = app.globalData.postDataArr;
    if(that.data.is_store_disable){
      let shop_arr = postDataArr.map(function(item) {
        let newitem={};
        newitem.good_id = 0;
        newitem.pr_name = item.trade_name;
        newitem.pr_alias = item.trade_name;
        newitem.pr_num = item.pr_num;
        newitem.true_unit = 0;
        newitem.pr_unit = 0;
        newitem.error_unit_name_show = item.unit_name;
        newitem.unit_price = 0;
        newitem.detail_remark = '';
        return newitem;
      });
      that.deal_with(shop_arr);
    }else{
      wx.showLoading({
        title:'生成中...',
      });
      app.AppHttp('api/Orders/YuYinOrCopyOrderPrview', {
        supplier_id:that.data.supplier_id,
        client_id:that.data.client_id,
        order_id:that.data.order_id,
        postData:JSON.stringify(postDataArr)
      }, function(res) {
        let goodList = res.data.map(function(item) {
          let newItem={
            good_id:item.good_id,
            supplier_id:item.supplier_id,
            client_id:item.client_id,
            pr_name:item.pr_name,
            specs:item.specs,
            pr_cover:item.pr_cover,
            pr_num:item.pr_num,
            pr_unit:item.pr_unit,
            true_unit:item.true_unit,
            original_unit:item.original_unit,
            sub_unit_num:item.sub_unit_num,
            error_unit_name_show:item.error_unit_name_show,
            detail_remark:item.detail_remark,
            unit_price:item.unit_price,
            is_ku:item.is_ku
          };
          return newItem;     
        });
        that.deal_with(goodList);
        wx.hideLoading();       
      });
    };
  },
  // 数据处理
  deal_with(goodList){
    let that = this;
    if(goodList.length>0){
      let daiNormalStorage= JSON.parse(wx.getStorageSync('dai_normal'));
      console.log(daiNormalStorage);
      console.log(goodList);
      daiNormalStorage[that.data.supplier_id]=goodList;
      wx.setStorageSync('dai_normal',JSON.stringify(daiNormalStorage));
      this.updataList();                   
    };
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      is_create_success:false
    });
    if(this.data.order_id>0){  //已确定订单id
      this.updataList();
      this.setData({
        isInput:false,
        isinput_col:false
      }) 
      if(this.data.is_replace){
        this.setData({
          page:1,
          allpage:1,
          load:'搜索中...'
        });
        this.upshoplist();
      };
    };
  },
  // 创建或获取待编辑单信息
  get_record(){
    var that = this;
    app.AppHttp('api/Orders/getClientSupplierEditingOrder', { 
      client_id:app.globalData.loginInfo.id,
      supplier_id:that.data.supplier_id
    },function(res){
      if(res.data.status==1){ //判断供应商是否有待编辑的单
        that.setData({
          order_id:res.data.data.id
        });
        if(that.data.is_again==1){
          that.again_fun();
        }else{
          that.updataList(); //调该订单的详情接口
        };
      }else{
        that.createOrder(); //创建订单
      };
    });
  },
  // 订单详情接口
  updataList(){
    let that = this;
    wx.showLoading({
      title:'加载中...',
    });
    app.AppHttp('api/Orders/getDaiOrderDetail', {id:that.data.order_id}, function (res) {
      let goodList=res.data.goodList;
      if(goodList.length>0){
        let detail_ids=goodList.map(function(item){
          return item.id;
        });
        detail_ids=detail_ids.join(',');
        that.del_shop_storage(detail_ids); //删除原有数据
      };
      let daiNormalStorage= wx.getStorageSync('dai_normal');
      //判断是否有缓存数据
      if(daiNormalStorage){
        daiNormalStorage= JSON.parse(daiNormalStorage);
        if(daiNormalStorage[that.data.supplier_id]){ //判断当前供应商是否存有数据
          goodList=daiNormalStorage[that.data.supplier_id];    
        }else{
          daiNormalStorage[that.data.supplier_id] = goodList;
        };
      }else{
        daiNormalStorage = {};
        daiNormalStorage[that.data.supplier_id] =goodList;
      };
      wx.setStorageSync('dai_normal',JSON.stringify(daiNormalStorage));
      let goodList_obj = {};
      let is_error_unit = 0;
      let is_two_error_unit = 0;
      goodList.forEach(function(item){
        if(item.good_id==0){
          is_error_unit++;
        };
        if(findKey(that.data.unit_list,item.error_unit_name_show)>0){
          item.pr_unit=findKey(that.data.unit_list,item.error_unit_name_show);
        }else{
          is_two_error_unit++;
        };
        if(item.good_id>0){
          if(goodList_obj[item.good_id]){
            if(goodList_obj[item.good_id]['unit_obj'][item.error_unit_name_show]){
              goodList_obj[item.good_id]['unit_obj'][item.error_unit_name_show]['pr_num']=item.pr_num;
              goodList_obj[item.good_id]['unit_obj'][item.error_unit_name_show]['detail_remark']=item.detail_remark;
            }else{
              goodList_obj[item.good_id]['unit_obj'][item.error_unit_name_show]={};
              goodList_obj[item.good_id]['unit_obj'][item.error_unit_name_show]['pr_num']=item.pr_num;
              goodList_obj[item.good_id]['unit_obj'][item.error_unit_name_show]['detail_remark']=item.detail_remark;
            }
          }else{
            goodList_obj[item.good_id]={};
            goodList_obj[item.good_id]['is_show']=true;
            goodList_obj[item.good_id]['unit_obj']={};
            goodList_obj[item.good_id]['unit_obj'][item.error_unit_name_show]={};
            goodList_obj[item.good_id]['unit_obj'][item.error_unit_name_show]['pr_num']=item.pr_num;
            goodList_obj[item.good_id]['unit_obj'][item.error_unit_name_show]['detail_remark']=item.detail_remark;
          }
        };
      });
      that.setData({
        info: res.data,
        is_show_hint_error:false,
        is_error_unit:is_error_unit,
        is_two_error_unit:is_two_error_unit,
        goodList: goodList,
        goodList_obj:goodList_obj
      });
      console.log(JSON.stringify(goodList));
      if(that.data.is_replace){
        that.setData({
          item_info:goodList[that.data.shopIndex]
        });
      };
      setTimeout(function(){
        const query = that.createSelectorQuery();
        query.select('.dai_head_three').boundingClientRect();
        query.select('.cai_btm').boundingClientRect();
        query.exec((res) => {
          that.setData({
            page_top_limit:res[0].bottom+that.data.item_h/2+20,
            page_btm_limit:res[1].top
          });
        });
      },1000)
      wx.stopPullDownRefresh();
      wx.hideLoading();
    })
  },
  // 删除原来存在于订单的数据，保证goodList均为前段数据
  del_shop_storage(detail_ids){
    app.AppHttp('api/Orders/delOrderDetail', {
      detail_id: detail_ids
    }, function (res) {   
    })
  },
  // 创建订单
  createOrder(){
    var that = this;
    app.AppHttp('api/Orders/nextCreateOrder', { 
      openid:app.globalData.openid, 
      client_id:app.globalData.loginInfo.id,
      supplier_id:that.data.supplier_id,
      type:1,
      is_substitute:0,
      port_name:'client'
    },function(res){
      if(res.data.status == 1){
        that.setData({
          order_id:res.data.data
        });
        if(that.data.is_again==1){
          that.again_fun();
        }else{
          that.updataList();
        };
      }else{
        wx.showToast({
          title: res.data.msg,
        });
      }
    });
  },
  /***
   * 按照输入的名称搜索产品
   */
  upshoplist(){
    var that = this;
    wx.showLoading({
      title:that.data.load
    });
    that.setData({
      loading:1
    });
    app.AppHttp('api/Order/SearchOrderGood', {
      keyword:that.data.shangpin,
      supplier_id: that.data.supplier_id,
      order_id: that.data.order_id,
      page: that.data.page,
      limit: 14
    }, function (res) {
      if(res.data.list.length>0){
        that.setData({
          good_dir_set:res.data.groupFind.good_dir_set,
          client_is_store:res.data.groupFind.client_is_store,
          purchase_type:res.data.groupFind.purchase_type,
        });
      };
      if (that.data.page == 1) {
        that.setData({
          loading:0,
          price_type:res.data.price_type,     //<3 非询价  ==3询价
          show_price_type:res.data.show_price_type,
          rightDatas: res.data.list,
          allpage: res.data.page_count,
        });
      } else {
        that.setData({
          loading:0,
          price_type:res.data.price_type,     //<3 非询价  ==3询价
          show_price_type:res.data.show_price_type,
          rightDatas: that.data.rightDatas.concat(res.data.list),
          allpage: res.data.page_count,       
        });
      };
      wx.hideLoading()
    });
  },
  // 替换弹窗滑动到底部
  replace_scroll_but(){
    if (this.data.page < this.data.allpage) {
      this.setData({
        page: this.data.page + 1,
        load: '加载更多'
      });
      this.upshoplist();
    } else {
      this.setData({
        loading:0
      });
      wx.showToast({
        title: '没有了'
      });
    };
  },
  // 编辑排序
  editSort(){
    this.setData({
      isEditSort:true
    });
  },
  // 完成编辑排序
  finEditSort(){
    this.setData({
      isEditSort:false
    });
  },
  // 替换操作
  openReplace(e){
    let pr_info = e.currentTarget.dataset.item;
    util.showBuyModal(this,1);
    this.setData({
      page:1,
      allpage:1,
      load:'搜索中...',
      scrollTop:0,
      is_replace:2,  
      item_info: pr_info,
      shopIndex:e.currentTarget.dataset.index,
      shangpin:pr_info.pr_name,
    });
    this.upshoplist();
  },
  /**
   * 清空输入空内容
   */
  clearClick(){
    var that = this;
    setTimeout(function (){
      that.setData({
        shangpin:'',
        rightDatas:[],
        page: 1,
        load: "搜索中"
      })
    },100);
  },
  /**
   * 关闭替换弹窗
   */
  replaceClose(){
    util.hideBuyModal(this,"is_replace",2);
    this.setData({
      shangpin:'',
      rightDatas:[],
    })
  },
  /**
   * 输入要搜索的产品名称
   */
  replaceSearch(e){
    this.setData({
      page:1,
      allpage:1,
      load:'搜索中...',
      scrollTop:0,
      shangpin:e.detail.value
    });
    app.globalData.task.abort();  //中断上一次搜索
    this.upshoplist();
  },
  replaceConfirm(){
    this.setData({
      page:1,
      allpage:1,
      load:'搜索中...',
      scrollTop:0
    });
    this.upshoplist();
  },
  replaceSearTap(){
    this.setData({
      page:1,
      allpage:1,
      load:'搜索中...',
      scrollTop:0
    });
    this.upshoplist();
  },
  /**
   * 打开添加商品弹窗
   */
  shopNumInput(e){
    let that = this;
    let index = e.currentTarget.dataset.id;
    util.showBuyModal(this); 
    let info = that.data.goodList[index];
    that.setData({
      content:'',
      upshop:0,
      tszhcontent:info.detail_remark?info.detail_remark:'',
      isInput:true,
      inputPriceA:'',
      shopIndex: index,
      inputNum: '',
      tanimg: info.pr_cover,
      shopMainid:info.good_id,
      shopid:info.good_id,
      tanname: info.pr_name,
      tanalias:info.pr_alias,
      tanspecs:info.specs,
      tanunit:info.error_unit_name_show,
      tannum: info.pr_num,
      tan_true_price:info.unit_price
    }); 
  },
  // 修改数量
  shop_confirm(){
    if (this.data.inputNum == '') {
      this.setData({
        inputNum:this.data.tannum
      });
    }
    var info = this.data.goodList[this.data.shopIndex];
    if (this.data.inputNum>9999.99){
      wx.showToast({
        title: '数量过大',
      })
      return;
    }
    info.pr_num = this.data.inputNum == '' ? 0 : this.data.inputNum;
    if(info.pr_num==0){
      wx.showLoading({
        title: '输入不能为0',
        duration: 500
      });
      return;
    }; 
    info.subtotal = (info.pr_num*info.unit_price*1).toFixed(2);
    info.singlePriceTotal = (info.pr_num*info.single_price*1).toFixed(2);
    info.detail_remark = this.data.tszhcontent;
    util.hideBuyModal(this,"isInput");
    let goodList_obj=this.data.goodList_obj;
    if(info.true_unit>0){
      goodList_obj[info.good_id].unit_obj[info.error_unit_name_show]['pr_num']=info.pr_num;
    };
    this.setData({
      ["goodList[" + this.data.shopIndex + "]"]: info,
      goodList_obj:goodList_obj
    });
    let daiNormalStorage= JSON.parse(wx.getStorageSync('dai_normal'));
    daiNormalStorage[this.data.supplier_id]=this.data.goodList;
    wx.setStorageSync('dai_normal',JSON.stringify(daiNormalStorage));
  },
  confirm(){
    if (this.data.inputNum == '') {
      this.setData({
        inputNum: this.data.tannum
      });
    }
    var info = this.data.rightDatas[this.data.inputIndex];
    if (this.data.inputNum>9999.99){
      wx.showToast({
        title: '数量过大',
      })
      return;
    }
    info.pr_num = this.data.inputNum == '' ? 0 : this.data.inputNum * 1;
    if(info.pr_num==0){
      wx.showLoading({
        title:'输入不能为0',
        duration:500
      });
      return;
    };
    var aux_obj={'is_aux':0,'pr_unit':this.data.tanunit_index,'unit':this.data.tanunit_index,'pr_unit_name':this.data.tanunit,'sub_unit_num':1};
    if(info.price_type==1 || info.price_type==2){
      if(this.data.price_type==2){ //客户级别价
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          if(this.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=this.data.aux_pf_list[this.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'unit':this.data.tanunit_index,'pr_unit_name':aux_item.sub_unit_name,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price
          });
        }else{  //非辅助单位
          this.setData({
            unit_price:info.level_true_price,
            single_price:info.level_single_price
          });
        }
      }else if(this.data.price_type==1){ //客户历史价
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          if(this.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=this.data.aux_pf_list[this.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'unit':this.data.tanunit_index,'pr_unit_name':aux_item.sub_unit_name,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price
          });
        }else{  //非辅助单位
          this.setData({
            unit_price:0,
            single_price:0
          });
        }
      }else if(this.data.price_type==4){ //客户固定价
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          if(this.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=this.data.aux_pf_list[this.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'unit':this.data.tanunit_index,'pr_unit_name':aux_item.sub_unit_name,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price
          });
        }else{  //非辅助单位
          this.setData({
            unit_price:info.true_price,
            single_price:info.single_price
          });   
        };
      }else{   //客户询价
        this.setData({
          unit_price:0,
          single_price:0
        });
      }
    }else if(info.price_type==3){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5
        if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
          return;
        };
        var assign_item=this.data.assign_superlist[this.data.assign_super_index];
        var nor_price_obj={'true_price':info.true_price,'single_price':0};
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'unit':this.data.tanunit_index,'pr_unit_name':assign_item.sub_unit_name,'sub_unit_num':assign_item.sub_unit_num};
          nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
        };
        this.setData({
          unit_price:nor_price_obj.true_price,
          single_price:nor_price_obj.single_price,
          shop_submit_price:assign_item.submit_price
        });   
      }else{   //其他模式
        this.setData({
          unit_price:info.true_price,
          single_price:0
        });
      }
    }else if(info.price_type==4){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5
        if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
          return;
        };
        var assign_item=this.data.assign_superlist[this.data.assign_super_index];
        var nor_price_obj={'true_price':assign_item.unit_price,'single_price':assign_item.unit_price};
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'unit':this.data.tanunit_index,'pr_unit_name':assign_item.sub_unit_name,'sub_unit_num':assign_item.sub_unit_num};
          nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
        };
        this.setData({
          unit_price:nor_price_obj.true_price,
          single_price:nor_price_obj.single_price,
          shop_submit_price:assign_item.submit_price
        });
      }else{  //其他模式
        this.setData({
          unit_price:info.true_price,
          single_price:info.true_price
        });
      }
    }else if(info.price_type==5){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5  
        if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
          return;
        };
        var assign_item=this.data.assign_superlist[this.data.assign_super_index];
        var nor_price_obj={'true_price':assign_item.unit_price,'single_price':assign_item.unit_price};
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'unit':this.data.tanunit_index,'pr_unit_name':assign_item.sub_unit_name,'sub_unit_num':assign_item.sub_unit_num};
          nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
        };
        this.setData({
          unit_price:nor_price_obj.true_price,
          single_price:nor_price_obj.single_price,
          shop_submit_price:assign_item.submit_price
        }); 
      }else{  //其他模式
        this.setData({
          unit_price:info.sale_price,
          single_price:info.sale_price
        });
      };
    };
    let goodList=this.data.goodList;
    let unit_price = this.data.single_price>0?this.data.single_price:this.data.unit_price;
    let newItem={
      good_id:info.id,
      supplier_id:this.data.supplier_id,
      client_id:this.data.info.client_id,
      pr_name:info.trade_name,
      specs:info.specs,
      pr_cover:info.cover,
      pr_num:info.pr_num,
      pr_unit:aux_obj.pr_unit,
      true_unit:aux_obj.pr_unit,
      original_unit:aux_obj.unit,
      sub_unit_num:aux_obj.sub_unit_num,
      error_unit_name_show:aux_obj.pr_unit_name,
      detail_remark:this.data.tszhcontent,
      unit_price:unit_price,
      is_ku:info.is_ku
    };
    let that = this;
    let isaufocus=true;
    // confirm方法里执行的都是单个单位
    if(this.data.is_replace){ //替换
      if(this.data.goodList_obj[info.id] && this.data.goodList_obj[info.id].is_show){ //修改
        let is_have=goodList.some(function( item,index ){
          if(info.id==item.good_id && aux_obj.pr_unit_name==item.error_unit_name_show){
            if(index != that.data.shopIndex){
              return true;
            }
          };
        });
        if(is_have){
          wx.showModal({
            title: '提示',
            content:"订单中不能出现多个相同单位的相同商品",
            showCancel: false,
            confirmText:"我知道了",
            success: function (res) {}
          });
          return;
        };
      };
      isaufocus=false;
      goodList[this.data.shopIndex]=newItem;
    }else{
      if(this.data.goodList_obj[info.id] && this.data.goodList_obj[info.id].is_show){ //修改
        let findIndex=goodList.findIndex(function( item ){
          if(info.id==item.good_id && aux_obj.pr_unit_name==item.error_unit_name_show){
            return true;
          };
        });
        goodList[findIndex]=newItem;
      }else{   //新增
        goodList.push(newItem);
      };
    };
    let daiNormalStorage= JSON.parse(wx.getStorageSync('dai_normal'));
    daiNormalStorage[this.data.supplier_id]=goodList;
    wx.setStorageSync('dai_normal',JSON.stringify(daiNormalStorage));
    util.hideBuyModal(this,"isInput");
    util.hideBuyModal(this,"is_replace",2);
    wx.showLoading({
      title: '添加成功',
      duration: 500
    });
    this.setData({
      shangpin:'',
      isaufocus:isaufocus,
      ["rightDatas[" + this.data.inputIndex + "]"]: info,
      inputNum: "",
      shopid: info.id,
      num: info.pr_num,
      content:"",
      inputIndex: -1,
      shopIndex:-1,
      searcontent:'',
      tszhcontent: '',
    });
    this.updataList();
  },
  // 取消关闭添加商品弹窗
  cancel(){
    util.hideBuyModal(this,"isInput");
    this.setData({
      inputIndex: -1,
      inputNum: "",
      tszhcontent: '',
      tangui: "",
      content: ""
    });
  },
  itemAddClick(e){
    this.numInput(e)
  },
  numInput(e) {
    var index = e.currentTarget.id;
    var info = this.data.rightDatas[index];
    var is_up=e.currentTarget.dataset.is_up;   //是否下架
    var is_ku=e.currentTarget.dataset.is_ku;   //是否属于库存商品
    var tan_is_auxiliary=e.currentTarget.dataset.is_auxiliary;  //是否开启辅助单位
    if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){  //模式5
      if(is_up==0){
        return;
      };     
      this.setData({
        assign_superlist:[],
        is_move_gif:true,
        assign_super_index:0,
        tan_is_auxiliary:tan_is_auxiliary,
      });
      this.assign_super_fun(info.id,tan_is_auxiliary);       
    }else{
      if(info.price_type<=2){ //供应-批发
        this.setData({
          aux_pf_list:[],
          tan_is_auxiliary:tan_is_auxiliary
        })
        if(tan_is_auxiliary==0){
          this.setData({
            aux_pf_index:-1,
            is_move_gif:false
          })
        }else if(tan_is_auxiliary==1){ //辅助单位
          this.setData({
            aux_pf_index:0,
            is_move_gif:true
          })
          this.aux_gy_pf_fun(info.id);
        };
      }else{
        this.setData({
          tan_is_auxiliary:tan_is_auxiliary,
          assign_super_index:-1,
          is_move_gif:false
        });
      }
    };
    util.showBuyModal(this);
    let tszhcontent='';
    let inputNum='';
    if(!this.data.is_replace){
      tszhcontent=info.detail_remark?info.detail_remark:'';
    }else{
      inputNum = this.data.item_info.pr_num;
      tszhcontent=this.data.item_info.detail_remark?this.data.item_info.detail_remark:'';
    };
    let tan_true_price='';
    let tan_single_price='';
    if(this.data.price_type==2){
      tan_true_price=info.level_true_price;
      tan_single_price=info.level_single_price;
    }else{
      tan_true_price=info.true_price;
      tan_single_price=info.single_price;
    };
    this.setData({
      tan_shop_ku:is_ku,
      upshop:1,
      content:'',
      isaufocus:false,
      isInput:true,
      inputPriceA:'',
      tszhcontent:tszhcontent,
      inputIndex: index,
      inputNum: inputNum,
      tanimg: info.cover,
      shopMainid:info.id,  //主规格id
      shopid:info.id,
      tanname: info.trade_name,
      tanalias:info.trade_alias,
      tanspecs:info.specs,
      tanunit_index: info.help_unit>0?info.help_unit:info.unit,
      tanunit: info.help_unit>0?info.help_unit_name:info.unit_name,
      tanunit_init:info.unit_name,
      tannum:this.data.goodList_obj[info.id] && this.data.goodList_obj[info.id].is_show?this.data.goodList_obj[info.id].unit_obj[Object.keys(this.data.goodList_obj[info.id].unit_obj)[0]]['pr_num']:0,
      tan_price_type:info.price_type,
      tan_true_price:tan_true_price,
      tan_single_price:tan_single_price,
      tan_sale_price:info.sale_price,
      shop_is_same:info.is_same //是否开启了采销同价
    }); 
  },
  /**
  * 删除订单的商品
  */
  deleteOutOrder(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let goodList = that.data.goodList;
    let goodList_obj = that.data.goodList_obj;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          if(goodList[index].good_id>0){
            delete goodList_obj[goodList[index].good_id].unit_obj[goodList[index].error_unit_name_show];
            if(Object.keys(goodList_obj[goodList[index].good_id].unit_obj)==0){
              delete goodList_obj[goodList[index].good_id]
            };
          };
          goodList.splice(index,1);
          let is_error_unit = 0;
          let is_two_error_unit = 0;
          goodList.forEach(function(item){
            if(item.good_id==0){
              is_error_unit++;
            };
            if(findKey(that.data.unit_list,item.error_unit_name_show)>0){
              item.pr_unit=findKey(that.data.unit_list,item.error_unit_name_show);
            }else{
              is_two_error_unit++;
            };
          });
          let daiNormalStorage= JSON.parse(wx.getStorageSync('dai_normal'));
          daiNormalStorage[that.data.supplier_id]=goodList;
          wx.setStorageSync('dai_normal',JSON.stringify(daiNormalStorage));
          that.setData({
            is_error_unit:is_error_unit,
            is_two_error_unit:is_two_error_unit,
            is_show_hint_error:false,
            goodList_obj:goodList_obj,
            goodList:goodList
          });
        };
      }
    })
  },
  // 清空订单
  clearOrderTap(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要清空订单吗？',
      success: function (sm) {
        if (sm.confirm) {
          let daiNormalStorage= JSON.parse(wx.getStorageSync('dai_normal'));
          daiNormalStorage[that.data.supplier_id]=[];
          wx.setStorageSync('dai_normal',JSON.stringify(daiNormalStorage));
          that.setData({
            goodList:[],
            goodList_obj:{}
          });
        };
      }
    })
  },
  // 键盘按键
  keyTap(e) {
    util.keyfunction(e,this,2);
    if(this.data.isinput_col==true){
      this.col_setValue();
    }else{
      this.setValue();
    };
  },
  setValue() {
    this.setData({
      inputNum: this.data.content
    });
  },
  col_setValue(){
    this.setData({
      shopnum: this.data.content
    });
  },
  // 去录入生成
  toVoice(e){
    let type = e.currentTarget.dataset.type;
    let index='';
    if(type==2){
      wx.setStorageSync('dai',JSON.stringify(this.data.goodList)) //存
    }else if(type==3){
      if(e.currentTarget.dataset.islist==1){  //列表直达录入
        index =e.currentTarget.dataset.index;
        wx.setStorageSync('dai',JSON.stringify([this.data.goodList[index]])) //存
      }else{
        index = this.data.shopIndex;
        wx.setStorageSync('dai',JSON.stringify([this.data.item_info])) //存
      }
    };
    wx.navigateTo({
      url:'/record_space/pages/entry/entry?type='+type+'&order_id='+this.data.info.order_id+'&supplier_id='+this.data.info.supplier_id+'&client_id='+this.data.info.client_id+'&insert_index='+index+'&is_store_disable='+this.data.is_store_disable
    });
  },
  //供应商未报价
  zero_price_but(){
    wx.showLoading({
      title:'供应商未报价',
      duration:500
    })
  },
  // 显示提示错误指示
  show_hint_error(){
    this.setData({
      is_show_hint_error:true
    });
  },
  // 打开规格的收藏内容
  spesc_but(e){
    var index = e.currentTarget.id;
    var is_up=e.currentTarget.dataset.is_up;  //是否下架及供应商数量
    var specs_count=e.currentTarget.dataset.specs_count;  //是否多规格
    var tan_is_auxiliary=e.currentTarget.dataset.is_auxiliary;  //是否开启辅助单位
    var is_ku=e.currentTarget.dataset.is_ku;  //当前商品是否属于库存商品
    if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){
      if(is_up==0){
        return;
      };
      this.setData({
        tan_is_auxiliary:tan_is_auxiliary,
        assign_superlist:[]
      });
      if(specs_count==0){  //仅有主规格
        this.setData({
          is_move_gif:true
        });
        this.assign_super_fun(e.currentTarget.dataset.shopid,tan_is_auxiliary);
      }; 
    }else{
      if(this.data.rightDatas[index].price_type<=2){  //供应-批发
        this.setData({
          aux_pf_index:-1,
          aux_pf_list:[],
          is_move_gif:tan_is_auxiliary==1?true:false,
          tan_is_auxiliary:tan_is_auxiliary,
        })
        if(tan_is_auxiliary==1 && specs_count==0){  //仅有主规格
          this.aux_gy_pf_fun(e.currentTarget.dataset.shopid);
        }; 
      }else{  //其他模式的企业
        this.setData({
          tan_is_auxiliary:tan_is_auxiliary,
          is_move_gif:false
        });
      }
    };
    this.setData({
      tan_shop_ku:is_ku,
      shopMainid:e.currentTarget.dataset.shopid,  //主规格id
      shopid: e.currentTarget.dataset.shopid,
      show_index:index,
      shop_is_same:this.data.rightDatas[index].is_same,
      ischoose_index: -1,
      tszhcontent: '',
      is_open_type:'other'
    });
    this.tancontent(); 
  },
  // 弹窗内容展示
  tancontent(){
    var that = this;
    app.AppHttp('api/Order/OrderGoodAlert', {
      id: that.data.shopid,
      order_id: that.data.order_id
    }, function (res) {
      that.setData({
        taninfo: res.data.data,    //弹窗数据   
        isinput_col:true,
        isaufocus:false,
        shopnum:'',
        shoppr_num:'',
      });
      if (res.data.data.length == 1) {
        that.setData({
          ischoose_index: 0,  //当规格只有一个时为选中状态
          tszhcontent: res.data.data[0].remark?res.data.data[0].remark:'', 
          shoppr_num: that.data.is_replace?that.data.item_info.pr_num:res.data.data[0].pr_num,  //选中的产品是否已添加
          shop_price_type:res.data.data[0].price_type,  //选中的产品价格类型
          shop_price:res.data.data[0].true_price, //选中的产品的价格
          shop_b_price:res.data.data[0].single_price,  //选中的产品B单价格
          shop_sale_price:res.data.data[0].sale_price,  //选中的产品销售价
          shop_level_price:res.data.data[0].level_true_price,  //级别A价
          shop_level_b_price:res.data.data[0].level_single_price  //级别B价
        });
      };
    });
  },
  // 选定条件后打开底部弹窗
  open_tan_info(e){
    if(this.data.ischoose_index>=0){
      util.showBuyModal(this);
      var taninfo = this.data.taninfo;
      let tanunit=taninfo[this.data.ischoose_index].unit_name;
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5
        if(this.data.assign_superlist.length==1){  //仅一个供应商
          if(this.data.tan_is_auxiliary==1){
            tanunit=this.data.assign_superlist[0].sub_unit_name;
          };
          this.setData({
            assign_super_index:0,
            shoppr_num:this.data.goodList_obj[this.data.shopid] && this.data.goodList_obj[this.data.shopid].unit_obj[tanunit]?this.data.goodList_obj[this.data.shopid].unit_obj[tanunit]['pr_num']:0
          });
        }else{
          if(this.data.tan_is_auxiliary==1){
            tanunit=this.data.assign_superlist[e.currentTarget.dataset.index].sub_unit_name;
          };
          this.setData({
            shoppr_num:this.data.goodList_obj[this.data.shopid] && this.data.goodList_obj[this.data.shopid].unit_obj[this.data.tanunit]?this.data.goodList_obj[this.data.shopid].unit_obj[this.data.tanunit]['pr_num']:0,
            assign_super_index:e.currentTarget.dataset.index
          });  
        }  
      }else{
        if(taninfo[this.data.ischoose_index].price_type<=2){  //供应-批发
          if(this.data.tan_is_auxiliary==0){
            this.setData({
              aux_pf_index:-1,
              aux_pf_list:[],
            });
          }else if(this.data.tan_is_auxiliary==1){
            if(this.data.aux_pf_list.length==1){   //仅一条辅助单位
              if(this.data.tan_is_auxiliary==1){
                tanunit=this.data.aux_pf_list[0].sub_unit_name;
              };
              this.setData({
                aux_pf_index:0,
                shoppr_num:this.data.goodList_obj[this.data.shopid] && this.data.goodList_obj[this.data.shopid].unit_obj[tanunit]?this.data.goodList_obj[this.data.shopid].unit_obj[tanunit]['pr_num']:0
              })
            }else{
              if(this.data.tan_is_auxiliary==1){
                tanunit=this.data.aux_pf_list[e.currentTarget.dataset.index].sub_unit_name;
              };
              this.setData({
                aux_pf_index:e.currentTarget.dataset.index,
                shoppr_num:this.data.goodList_obj[this.data.shopid] && this.data.goodList_obj[this.data.shopid].unit_obj[tanunit]?this.data.goodList_obj[this.data.shopid].unit_obj[tanunit]['pr_num']:0
              })
            }
          }
        }
      };
      let tan_true_price='';
      let tan_single_price='';
      if(this.data.price_type==2){
        tan_true_price=taninfo[this.data.ischoose_index].level_true_price;
        tan_single_price=taninfo[this.data.ischoose_index].level_single_price;
      }else{
        tan_true_price=taninfo[this.data.ischoose_index].true_price;
        tan_single_price=taninfo[this.data.ischoose_index].single_price;
      };
      let tszhcontent = '';
      if(this.data.is_replace){
        tszhcontent=this.data.item_info.detail_remark?this.data.item_info.detail_remark:'';
      };
      this.setData({
        upshop:1,
        content:'',
        isInput:true,
        inputPriceA:'',
        tszhcontent:tszhcontent,
        shoppr_num:this.data.is_replace?this.data.item_info.pr_num:this.data.shoppr_num,
        tanimg: taninfo[this.data.ischoose_index].cover,
        tanname: taninfo[this.data.ischoose_index].trade_name,
        tanalias:taninfo[this.data.ischoose_index].trade_alias,
        tanspecs:taninfo[this.data.ischoose_index].specs,
        tanunit_index:taninfo[this.data.ischoose_index].help_unit>0?taninfo[this.data.ischoose_index].help_unit:taninfo[this.data.ischoose_index].unit,
        tanunit:taninfo[this.data.ischoose_index].help_unit>0?taninfo[this.data.ischoose_index].help_unit_name:taninfo[this.data.ischoose_index].unit_name,
        tanunit_init:taninfo[this.data.ischoose_index].unit_name,
        tan_true_price:tan_true_price,
        tan_single_price:tan_single_price, 
        tan_sale_price:taninfo[this.data.ischoose_index].sale_price,
        tan_price_type:taninfo[this.data.ischoose_index].price_type
      });
    }else{
      wx.showLoading({
        title:'请选择规格',
        duration:500
      })
    };
  },
  // 仅添加或添加并收藏的接口
  addorder(e) {
    let that = this;
    var shopnum = this.data.shopnum;
    if (this.data.ischoose_index == -1) {
      wx.showLoading({
        title: '请选择规格',
        duration: 500
      });
      return;
    }else if(that.data.shopnum==0){
      if(that.data.shopnum != ''){ //输入0
        shopnum=0;
      }else if(that.data.shopnum == ''){ //不输入
        shopnum = that.data.shoppr_num;
      };
      if(shopnum==0){
        wx.showLoading({
          title:'输入不能为0',
          duration:500
        });
        return;
      };
    };
    if (shopnum > 9999.99) {
      wx.showToast({
        title: '数量过大'
      })
      return;
    };
    var aux_obj={'is_aux':0,'pr_unit':that.data.tanunit_index,'unit':that.data.tanunit_index,'pr_unit_name':that.data.tanunit,'sub_unit_num':1};
    if(this.data.shop_price_type==1 || this.data.shop_price_type==2){
      if(this.data.price_type==2){ //客户级别价
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'unit':that.data.tanunit_index,'pr_unit_name':aux_item.sub_unit_name,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            num: shopnum,
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price,
          });
        }else{  //非辅助单位
          this.setData({
            num: shopnum,
            unit_price:this.data.shop_level_price,
            single_price:this.data.shop_level_b_price
          });
        }
      }else if(this.data.price_type==1){ //客户历史价
        if(this.data.tan_is_auxiliary==1){ //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'unit':that.data.tanunit_index,'pr_unit_name':aux_item.sub_unit_name,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            num: shopnum,
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price,
          });
        }else{  //非辅助单位
          this.setData({
            num: shopnum,
            unit_price:0,
            single_price:0
          });
        }
      }else if(that.data.price_type==4){  //客户固定价
        if(this.data.tan_is_auxiliary==1){ //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'unit':that.data.tanunit_index,'pr_unit_name':aux_item.sub_unit_name,'sub_unit_num':aux_item.sub_unit_num};
          this.setData({
            num: shopnum,
            unit_price:nor_price_obj.true_price,
            single_price:nor_price_obj.single_price,
          });
        }else{  //非辅助单位
          this.setData({
            num: shopnum,
            unit_price:this.data.shop_price,
            single_price:this.data.shop_b_price
          });
        };
      }else{  //客户询价
        this.setData({
          num: shopnum,
          unit_price:0,
          single_price:0
        });
      }
    }else if(this.data.shop_price_type==3){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5 
        if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
          return;
        };
        var assign_item=that.data.assign_superlist[that.data.assign_super_index];
        var nor_price_obj={'true_price':that.data.shop_price,'single_price':0};
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'unit':that.data.tanunit_index,'pr_unit_name':assign_item.sub_unit_name,'sub_unit_num':assign_item.sub_unit_num};
          nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
        };
        this.setData({
          num: shopnum,
          unit_price:nor_price_obj.true_price,
          single_price:nor_price_obj.single_price,
          shop_submit_price:assign_item.submit_price
        });
      }else{  //其他模式
        this.setData({
          num: shopnum,
          unit_price:this.data.shop_price,
          single_price:0
        });
      };
    }else if(this.data.shop_price_type==4){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){//模式5
        if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
          return;
        };
        var assign_item=that.data.assign_superlist[that.data.assign_super_index];
        var nor_price_obj={'true_price':assign_item.unit_price,'single_price':assign_item.unit_price};
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'unit':that.data.tanunit_index,'pr_unit_name':assign_item.sub_unit_name,'sub_unit_num':assign_item.sub_unit_num};
          nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
        };
        this.setData({
          num: shopnum,
          unit_price:nor_price_obj.true_price,
          single_price:nor_price_obj.single_price,
          shop_submit_price:assign_item.submit_price
        });
      }else{  //其他模式
        this.setData({
          num: shopnum,
          unit_price:this.data.shop_price,
          single_price:this.data.shop_price
        });
      };
    }else if(this.data.shop_price_type==5){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){//模式5 
        if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
          return;
        };
        var assign_item=that.data.assign_superlist[that.data.assign_super_index];
        var nor_price_obj={'true_price':assign_item.unit_price,'single_price':assign_item.unit_price};
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'unit':that.data.tanunit_index,'pr_unit_name':assign_item.sub_unit_name,'sub_unit_num':assign_item.sub_unit_num};
          nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
        };
        this.setData({
          num: shopnum,
          unit_price:nor_price_obj.true_price,
          single_price:nor_price_obj.single_price,
          shop_submit_price:assign_item.submit_price
        }); 
      }else{  //其他模式
        this.setData({
          num: shopnum,
          unit_price:this.data.shop_sale_price,
          single_price:this.data.shop_sale_price
        });
      };
    };
    let goodList=this.data.goodList;
    let unit_price = this.data.single_price>0?this.data.single_price:this.data.unit_price;
    let newItem={
      good_id:this.data.shopid,
      supplier_id:this.data.supplier_id,
      client_id:this.data.info.client_id,
      pr_name:this.data.tanname,
      specs:this.data.tanspecs,
      pr_cover:this.data.tanimg,
      pr_num:shopnum,
      pr_unit:aux_obj.pr_unit,
      true_unit:aux_obj.pr_unit,
      original_unit:aux_obj.unit,
      sub_unit_num:aux_obj.sub_unit_num,
      error_unit_name_show:aux_obj.pr_unit_name,
      detail_remark:this.data.tszhcontent,
      unit_price:unit_price,
      is_ku:this.data.tan_shop_ku
    };
    let isaufocus=true;
    if(this.data.is_replace){ //替换
      if(this.data.goodList_obj[this.data.shopid] && this.data.goodList_obj[this.data.shopid].is_show){
        if(this.data.goodList_obj[this.data.shopid].unit_obj[aux_obj.pr_unit_name]){
          let is_have=goodList.some(function(item,index){
            if(that.data.shopid==item.good_id && aux_obj.pr_unit_name==item.error_unit_name_show){
              if(index != that.data.shopIndex){
                return true;
              }
            };
          });
          if(is_have){
            wx.showModal({
              title: '提示',
              content:"订单中不能出现多个相同单位的相同商品",
              showCancel: false,
              confirmText:"我知道了",
              success: function (res) {}
            });
            return;
          };
        }
      };
      isaufocus=false;
      goodList[this.data.shopIndex]=newItem;   
    }else{
      //多单位的情况
      if(this.data.goodList_obj[this.data.shopid] && this.data.goodList_obj[this.data.shopid].is_show){  
        if(this.data.goodList_obj[this.data.shopid].unit_obj[aux_obj.pr_unit_name]){ //修改
          let findIndex=goodList.findIndex(function( item ){
            if(that.data.shopid==item.good_id && aux_obj.pr_unit_name==item.error_unit_name_show){
              return true;
            };
          });
          goodList[findIndex]=newItem;
        }else{  //新增
          goodList.push(newItem);
        }
      }else{   //新增
        goodList.push(newItem);
      };
    }
    let daiNormalStorage= JSON.parse(wx.getStorageSync('dai_normal'));
    daiNormalStorage[this.data.supplier_id]=goodList;
    wx.setStorageSync('dai_normal',JSON.stringify(daiNormalStorage));
    this.updataList();
    if(this.data.isInput==true){
      util.hideBuyModal(this,"isInput");
    };
    if(this.data.is_replace>0){
      util.hideBuyModal(this,"is_replace",2);
    };   
    this.setData({
      shangpin:'',
      isaufocus:isaufocus,
      isinput_col: false,
      inputNum: "",
      content:"",
      inputIndex: -1,
      searcontent:'',
      tszhcontent: '',
    });
  },
  /**
   * 取消添加商品
   */
  cancelNum(){
    this.setData({
      inputNum: '',
      isinput_col:false
    })
  },
  // 打开特殊需求备注弹窗
  opentszh() {
    this.setData({
      tszhinput: this.data.tszhcontent,
      istszh: true,
    })
  },
  // 特殊需求备注弹窗输入内容
  tszhinput(e){
    this.setData({
      tszhinput: e.detail.value
    })
  },
  // 关闭特殊需求备注弹窗
  tszhcancel(){
    this.setData({
      istszh: false
    })
  },
  // 确定并关闭需求备注弹窗
  tszhconfirm(){
    this.setData({
      istszh: false,
      tszhcontent: this.data.tszhinput
    })
  },
  /***
   * 关闭弹窗
   */
  close_bth(){
    this.setData({
      showModal:false,
      order_remark:''
    })
  },
  /**
   * 编辑备注点击
   */
  open_orderRemark(){
    this.setData({
      showModal: true,
      order_remark:this.data.info.order_remark
    });
  },
  /**
   * 设置备注
   */
  setRemark(e){
    this.setData({
      order_remark: e.detail.value
    });
  },
  /**
   * 确定修改备注
   */
  confirmRemark(){
    let that = this;
    let order_id = that.data.info.order_id;
    let order_remark = that.data.order_remark;
    if (that.data.order_remark !=''){
    app.AppHttp("api/Order/changeRemark", { id: order_id, order_remark: order_remark},function(res){
        if(res.data.status == 1){
          wx.showToast({
            title: res.data.msg,
            duration:500,
            success:function(){
              that.updataList();
              that.setData({
                showModal:false,
                order_remark:''
              })
            }
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'loading',
            duration:500,
          })
        }
    })
    }else{
      wx.showToast({
        title: '请输入备注',
        icon: 'loading',
        duration: 500,
      })
    }
  },
  // 选择菜品时的供应商和价格
  assign_super_fun(good_id,is_aux){
    var that = this; 
    app.AppHttp('api/Shopping/getGoodSupplierInfo', {
      order_id:that.data.order_id,
      client_id: that.data.info.client_id,
      good_id:good_id,
      is_aux:is_aux
    }, function (res) {
      var assign_superlist = res.data.filter(function (item) {
        return (that.data.info.supplier_id == item.supplier_id);
      });   
      that.setData({
        is_move_gif:false,
        assign_superlist:assign_superlist
      });   
      if(that.data.isinput_col && assign_superlist.length==1){   //当收藏弹窗显示时，才有可能调用
        that.open_tan_info();
      };   
    });
  },
  // 供应-批发端辅助单位接口
  aux_gy_pf_fun(good_id){
    var that = this; 
    app.AppHttp('api/Shopping/getSupplierGoodsAux', {
      order_id:that.data.order_id,
      client_id: that.data.info.client_id,
      good_id:good_id,
      supplier_id:that.data.info.supplier_id
    }, function (res) {
      let aux_pf_list=[];
      if(res.data.goodsAuxiliaryList.length>0){
        aux_pf_list = res.data.goodsAuxiliaryList.filter((c,i) => {
          if(c.is_order_unit==1){
            if(that.data.price_type==2){
              res.data.goodsAuxiliaryList[i].sub_price = res.data.goodsAuxiliaryList[i].level_sub_price;
              return true;
            }else{
              res.data.goodsAuxiliaryList[i].sub_price = res.data.goodsAuxiliaryList[i].his_sub_price;
              return true;
            };
          };
        });
      };
      that.setData({
        is_move_gif:false,
        aux_pf_list:aux_pf_list
      });
      if((that.data.price_type==1 || that.data.price_type==4) && aux_pf_list.length==0){
        that.setData({
          tan_is_auxiliary:0 //**有辅助单位，但还没有历史价，按无辅助单位处理
        })
        if(that.data.isinput_col){
          that.open_tan_info();
        }
      };
      if((that.data.isinput_col || that.data.is_open_type=='normal') && aux_pf_list.length==1){   //当收藏弹窗显示时，才有可能调用
        that.open_tan_info();
      };
    });  
  },
  /**
 * 完成下单
  */
  finishClick(){
    let that =this;
    wx.showLoading({
      title: '操作中...',
    })
    that.setData({
      showMask: true
    })
    let goodList = that.data.goodList;
    let order_id = that.data.info.order_id;
    if(goodList.length > 0){
      let port_str;
      if(that.data.is_store_disable){
        goodList = goodList.map(function(item) {
          let newitem={};
          let pinyin = ZXZ.widget.pinyin.convertPYs(item.pr_name)[1]+ZXZ.widget.pinyin.convertPYs(item.pr_name)[0].slice(0,8);
          newitem.good_id = "F"+pinyin+item.pr_unit;
          newitem.pr_name = item.pr_name;
          newitem.trade_alias = item.pr_alias;
          newitem.pr_num = item.pr_num;
          newitem.pr_unit = item.pr_unit;
          newitem.detail_remark = item.detail_remark;
          newitem.error_unit_name_show = item.error_unit_name_show;
          newitem.unit_price = item.unit_price;
          return newitem;
        });
        port_str = 'api/Orders/YuYinOrCopyOrderSubmitmxj';
      }else{
        port_str = 'api/Orders/YuYinOrCopyOrderSubmitfb';
      };
      app.AppHttp(port_str,{
        order_id:order_id,
        order_status:1,
        postData:JSON.stringify(goodList)
      },function(res){
        if(res.data.status==1){
          let cur_time = util.formatTime(new Date());
          cur_time=cur_time.slice(0,-3);
          let val_arr=(cur_time).split(/[/-]/g);
          cur_time=val_arr[0]+'年'+val_arr[1]+'月'+val_arr[2]+'日 '+val_arr[3];
          if(that.data.is_again==1){
            app.globalData.dingdan_show = 1;
            that.toOrderList();
          }else{
            that.setData({
              is_create_success:true,
              showMask:false,
              cur_time:cur_time  //当前时间
            });
          };
          // 获取数据
          let daiNormalStorage= JSON.parse(wx.getStorageSync('dai_normal'));
          // 处理数据
          delete daiNormalStorage[that.data.supplier_id];
          if(daiNormalStorage){
            // 存储数据
            wx.setStorageSync('dai_normal',JSON.stringify(daiNormalStorage));
          }else{
            wx.removeStorageSync('dai_normal');
          };
          that.setData({
            order_id:'',
            suc_count:goodList.length,
            goodList:[],  //商品列表
            goodList_obj:{},  //商品是否已添加(查询表)
          });
        }
      })
      wx.hideLoading();
    }else{
      that.setData({
        showMask: false
      })
      wx.showToast({
        title: '请添加商品',
        duration:2000,
        icon:'loading'
      })
    }
  },
  // 去订单列表
  toOrderList(){
    wx.switchTab({
      url: '/pages/dingdan/dingdan'
    });
  },
  // 关闭下单成功提示
  close_create_suc(){
    util.hideBuyModal(this,'is_create_success');
    this.get_record();
  },
  // 开始拖拽
  movestart(e){ 
    var index = e.currentTarget.dataset.index;
    y = e.touches[0].clientY;         //开始触摸坐标
    y1 = e.currentTarget.dataset.item_h;   //元素初始距顶部的距离
    this.setData({
      move_index: index,      //触摸移动元素的索引
      move_index_zero: index,   //开始触摸时的初始索引
      move_y:y1    //初始顶部位置
    });
  },
  // 拖拽中
  move(e){ 
    y2 = e.touches[0].clientY - y;      //触摸中移动距离
    y3 = y2+y1;      //移动中元素距顶部的距离
    if(y3>-this.data.item_h/2){
      var underIndex = this.getCurrnetUnderIndex(); 
    };  
    if (underIndex != null && underIndex != this.data.move_index) {
      var arr = this.data.goodList;
      this.changeArrayData(arr, underIndex, this.data.move_index);
      this.setData({
        goodList: arr,
        move_index: underIndex
      })
    }
    this.setData({
      move_y: y3
    })
  },
  // 结束拖拽
  moveend(e){
    this.setData({
      move_index: -1
    });
  },
  // 改变数组顺序
  changeArrayData(arr, i1, i2){
    var temp = arr[i1];
    arr[i1] = arr[i2];
    arr[i2] = temp;
  },
  // 计算移动元素下方的索引
  getCurrnetUnderIndex(){
    if (y2>=0){
      var endy = y2 + this.data.item_h/2;
    }else{
      var endy = y2 - this.data.item_h/2;
    };
    var addindex = parseInt(endy/this.data.item_h); 
    var underIndex = this.data.move_index_zero + addindex;
    if (underIndex > this.data.goodList.length - 1){
      return null;
    }else{
      return underIndex;
    }
  },
  noneEnoughPeople(){
    return;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})